package cc.vicp.djx314.starfood.web.personnel.order;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cc.vicp.djx314.starfood.domain.order.Order;
import cc.vicp.djx314.starfood.domain.order.OrderDish;
import cc.vicp.djx314.starfood.service.order.OrderService;

@Controller
public class EnabledOrderController {
	public static final Logger logger = LoggerFactory.getLogger(EnabledOrderController.class);

	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/personnel/enabled", method = RequestMethod.PUT)
	public ModelAndView enabledOrder(@RequestBody Order order) {
		ModelAndView model = new ModelAndView("jsontournamenttemplate");
		List<OrderDish> orderDishes = order.getOrderDishes();
		if (orderDishes != null && orderDishes.size() > 0) {
			for (OrderDish orderDish : orderDishes) {
				Order myOrder = new Order();
				myOrder.setId(order.getId());
				orderDish.setOrder(myOrder);
			}
		}
		boolean isSuccess = false;
		try {
			orderService.updateOrderEnabled(order);
			isSuccess = true;
		} catch (Exception e) {
			logger.info("确认订单过程出错", e);
		}
		model.addObject("success", isSuccess);
		return model;
	}
}